package com.tange.ai.core.demo;

import static com.tange.ai.core.demo.LoginActivity.PRE_USER_PWD;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;

import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.tange.ai.core.demo.databinding.ActivityMainBinding;
import com.tange.base.toolkit.PreferenceUtil;
import com.tange.core.backend.service.auth.OnUnauthorizedListener;
import com.tange.core.backend.service.auth.UnauthorizedDispatcher;

public class MainActivity extends AppCompatActivity {

    private ActivityMainBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        BottomNavigationView navView = findViewById(R.id.nav_view);
        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.
        AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
                R.id.navigation_home, R.id.navigation_notifications)
                .build();
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_activity_main);
        NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
        NavigationUI.setupWithNavController(binding.navView, navController);

        getSupportActionBar().hide();

        Utils.setStatusBar(this, Color.TRANSPARENT);

        UnauthorizedDispatcher.setListener((i, s) -> {
            PreferenceUtil.setString(MainActivity.this, PRE_USER_PWD, "");
            Toast.makeText(MainActivity.this, "登录态过期！请重新登录！", Toast.LENGTH_SHORT).show();
            startActivity(new Intent(MainActivity.this, LoginActivity.class));
        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.top_nav_menu, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        // Handle presses on the action bar items
        switch (item.getItemId()) {
            case R.id.navigation_add:

                return true;
            case R.id.navigation_logout:
                Toast.makeText(this, "Logout", Toast.LENGTH_LONG).show();
                PreferenceUtil.setString(MainActivity.this, LoginActivity.PRE_USER_NAME, "");
                PreferenceUtil.setString(MainActivity.this, PRE_USER_PWD, "");
                startActivity(new Intent(MainActivity.this, LoginActivity.class));
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
}